//todo:完善赋值影响全部class的问题.
function jiSuan(){
	$("#out_storage_table").on("click",".out_storage_sub",function (){
		$(this).each(function (i, item) {
			var maxValue = parseInt($(item).parents("td").children(":text").attr("placeholder").substring(2));
			var value = $(item).parents("td").children(":text").val();
			if (value != "") {
				if (value>1){
					$(item).parents("td").children(":text").val(value - 1 );
				}
			}
		});
	})
	$("#out_storage_table").on("click",".out_storage_sum",function (){
		$(this).each(function (i, item) {
			var maxValue = parseInt($(item).parents("td").children(":text").attr("placeholder").substring(2));
			var value = $(item).parents("td").children(":text").val();
			if (value != "") {
				if (value<maxValue){
					$(item).parents("td").children(":text").val(parseInt(value) + 1 );
				}
			}
		});
	})
}

$(function(){

	$("#out_storage_submit").on("click",function (){

		var submitList = $("#out_storage_form").serializeArray();
		//managerId
		var managerId = 0;
		var managerRealName = $("#manager").html()
		var manager = null;
		$.ajax({
			url:"/manager/findManagerByRealName",
			data:{realName:managerRealName},
			async:false,
			success:function (data){
				manager = data;
				managerId = manager.id;
			}
		})

		var outId = $("#out_storage_id").html();
		var userId = 0;
		//todo:ajax添加User用户并获取添加的ID值.
		userId = addUser();
		//todo:ajax添加manager用户
		var transport = $("#out_storage_transport").val();
		var payCondition = $("#out_storage_payCondition").val();
		var outStorageList = [];
		var index = 0 ;
		var outStorage;
		$.each(submitList, function (i, item) {
			if(i%8==0){
				 outStorage= {
					id:'',
					outId:outId,
					userId:userId,
					managerId:managerId,
				 	storageId:'',
					outDate:new Date(),
					varietyName:'',
					typeName:'',
					standardName:'',
					price:'',
					count:'',
					money:'',
					transport:transport,
					payCondition:payCondition
				}
			}
			console.log(item);
			switch (item.name){
				case 'varietyName':outStorage.varietyName = item.value;break;
				case 'storageId':outStorage.storageId = item.value;break;
				case 'typeName':outStorage.typeName = item.value;break;
				case 'standardName':outStorage.standardName = item.value;break;
				case 'price':outStorage.price = item.value;break;
				case 'count':outStorage.count = item.value;break;
				case 'money':outStorage.money = item.value;
					outStorageList[index] = outStorage;
					index++;
					break;
				default:break;
			}
			console.log(outStorage);
		});

		var listStr = JSON.stringify(outStorageList);
		console.log(listStr);
		$.ajax({
			url: "/outStorage/saveStorages",
			//上传json字符串必须设置的属性
			contentType: "application/json;charset=utf-8",
			type: "post",
			data: listStr,
			success: function (data) {
				if (data =='success'){
					location.href = "/outStorage/list";
				}else{
					document.write("添加失败")
				}
			}
		});


		return false;
	})

	$("#out_storage_table").on("change",".out_storage_count",function (){
		var var1 = $(this).val();
		var var2 = $(this).parents("tr").children().children(".out_storage_price").val();
		if (var1 != "" && var2 != ""){
			var result = parseInt(parseInt(var2)*parseFloat(var1)*100)/100;
			$(this).parents("tr").children().children(".out_storage_result").val(result);
		}
	})
	$("#out_storage_table").on("change",".out_storage_price",function (){
		var var1 = $(this).val();
		var var2 = $(this).parents("tr").children().children(".out_storage_count").val();
		if (var1 != "" && var2 != ""){
			var result = parseInt(parseInt(var2)*parseFloat(var1)*100)/100;
			$(this).parents("tr").children().children(".out_storage_result").val(result);
		}
	})


	$("#out_storage_table").on("blur",".out_storage_standard",function (){
		$(this).each(function (i , item){
			var variety = $(item).parents("td").children(".out_sotrage_company").val()
			var type = $(item).parents("td").children(".out_storage_type").val()
			var value = $(item).val();
			$.post("/outStorage/getOutStorageByCondition",{variety:variety,type:type,standard:value},function (data){
				for (let i in data) {
					$(item).parents("td").children("input:hidden").val(data[i].id);
					$(item).parents("tr").children().children(".out_storage_count").attr("placeholder","库存" + data[i].count);
				}
			})
		})
	})

	$("#out_storage_table").on("blur",".out_storage_type",function (){
		$(this).each(function (i , item){
			var variety = $(item).parents("td").children(".out_sotrage_company").val()
			var value = $(item).val();
			$.post("/outStorage/getOutStorageByCondition",{variety:variety,type:value},function (data){
				$("#out_storage_data_storage_standard").empty();
				for (let i in data) {
					$("#out_storage_data_storage_standard").append("<option value='"+data[i].standard+"'></option>");
				}
			})
		})
	})

	$("#out_storage_table").on("blur",".out_storage_company",function (){
		$(this).each(function (i , item){
			var value = $(item).val();
			$.post("/outStorage/getOutStorageByCondition",{variety:value},function (data){
				$("#out_storage_data_storage_type").empty();
				for (let i in data) {
					$("#out_storage_data_storage_type").append("<option value='"+data[i].type+"'></option>");

				}
			})
		})
	})


	jiSuan()

	$("#storage_btn_add").on("click",function (){
		$("#out_storage_table tbody").append("<tr>"+$("#out_storage_tr").html()+"</tr>")

	})

	$("#storage_btn_del").on("click",function (){
		$(":checkbox").each(function (i,item){
			if(item.checked==true){
				$(item).parents("tr").remove();
			}
		})
	})

	$("#out_storage_checkbox").on("click",function (){
		$(":checkbox").each(function (i, item) {
			if (item.checked == true) {
				item.checked = false;
			}else{
				item.checked = true;
			}
		});
		return false;
	})

	var date = new Date();
	var dateStr = ""+date.getFullYear()+toDoubleLocation(date.getMonth()+1)+toDoubleLocation(date.getDate())+toDoubleLocation(date.getHours())+toDoubleLocation(date.getMinutes())+toDoubleLocation(date.getSeconds());
	$("#out_storage_id").html(dateStr);
} )

/**
 * 双占位方法
 * @param data 时间的格式
 * @returns {string} 双位字符串
 */
function toDoubleLocation (data){
	var string = new String (data);
	if(string.length==1){
		return "0"+string;
	}else{
		return string;
	}
}


function addUser() {
	var company = $("#out_storage_company").val();
	var location = $("#out_storage_location").val();
	var contact = $("#out_storage_contact").val();
	var telephone = $("#out_storage_telephone").val();
	var id = 0 ;
	$.ajax({
		url:"/user/saveUser",
		async:false,
		data:{company:company,location:location,contact:contact,telephone:telephone},
		success:function (data){
			id = data;
		}
	})
	return id;
}